/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame.Object;

import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Spatial;
import mygame.Backbone.GlobalVars;
import mygame.Manager;
import mygame.Scene.PlacementTriangle;

/**
 *
 * @author Mr.N0I3oDy
 */
public abstract class GameObject {

    protected int id = -1;
    protected PlacementTriangle place;
    private int lid;//ListId of the Triangle
    protected Spatial model;
    protected String name;
    protected String ressource;
    protected int amount;

    public GameObject(String name) {
        this.name = name;
        this.place = null;
        this.model = null;
        this.amount = 1;
        String path = "";
        float scale = 0f;
        if (name.equals(GlobalVars.OBJECT_NAME_STONE)) {
            this.ressource = GlobalVars.OBJECT_NAME_STONE;
            path = GlobalVars.MODEL_PATH_STONE;
            scale = 0.4f;
        } else if (name.equals(GlobalVars.OBJECT_NAME_AST)) {
            this.ressource = GlobalVars.OBJECT_NAME_AST;
            path = GlobalVars.MODEL_PATH_AST;
            scale = 0.5f;
        } else if (name.equals(GlobalVars.OBJECT_NAME_STRAUCH)) {
            this.ressource = GlobalVars.OBJECT_NAME_BERRY;
            this.amount = GlobalVars.random.nextInt(2)+1;
            path = GlobalVars.MODEL_PATH_STRAUCH;
            scale = 0.6f;
        }
        model = Manager.GetInstance().getAssetManager().loadModel(path);

        this.id = (GlobalVars.ObjectCount++);
        Manager.GetInstance().getModelManager().InteractiveObjects.put(id, this);

        model.setUserData("GameObject_id", id);
        model.setLocalScale(scale);
    }

    public Spatial get3DModel() {
        return this.model;
    }

    public String getName() {
        return this.name;
    }

    public PlacementTriangle getPlace() {
        return this.place;
    }

    public void placeObject(PlacementTriangle place, int lid) {
        this.place = place;
        this.place.placeObject(this.model);
        this.lid = lid;
    }

    protected void removeObject() {
        Manager.GetInstance().getModelManager().InteractiveObjects.remove(this.id);
        removeFromPlace();
        Manager.GetInstance().getModelManager().detach(this.model);
    }

    private void removeFromPlace() {
        this.getPlace().removeObject();
        this.place = null;
    }

    public abstract void interact();

    public String save() {
        String save = GlobalVars.DELIMITER_OBJECTS;
        save += this.getClass().toString();
        save += GlobalVars.DELIMITER_ATTRIBUTE;
        save += this.name;
        save += ",";
        save += this.lid;
        save += "";
        return save;
    }

    public static void load(String s) {
        String[] temp = s.split(GlobalVars.DELIMITER_ATTRIBUTE);
        s = temp[1];
        String[] atr = s.split(",");

        String classname = temp[0];
        String name = atr[0];
        int lid = Integer.parseInt(atr[1]);


        if (classname.equals(Pickable.class.toString())) {
            new Pickable(name).placeObject(Manager.GetInstance().getSceneManager().getGroundTriangles().get(lid), lid);
        } else {
            new Grabable(name).placeObject(Manager.GetInstance().getSceneManager().getGroundTriangles().get(lid), lid);
        }
    }

    public String getRessource() {
        return this.ressource;
    }
}
